<template>
	<view class="container">
		<!-- 自定义导航栏 -->
		<view class="custom-nav">
			<view class="nav-content">
				<text class="title">个人中心</text>
				<view class="settings" @click="goSettings">
					<text style="font-size: 50rpx;" class="iconfont icon-shezhi"></text>
				</view>
			</view>
		</view>

		<!-- 用户信息 -->
		<view class="user-info" v-if="userInfo">
			<image :src="userInfo.avatar ? BASE_URL + userInfo.avatar : '/static/delault/user_at.png'" 
				   class="avatar"></image>
			<view class="info">
				<text class="username">{{userInfo.username}}</text>
				<text class="points">积分: {{userInfo.points || 0}}</text>
				<text class="join-time">加入时间: {{formatDate(userInfo.created_at)}}</text>
			</view>
		</view>
		
		<!-- 未登录状态 -->
		<view class="not-login" v-else @click="goLogin">
			<image src="/static/delault/user_at.png" class="avatar"></image>
			<text>请登录</text>
		</view>
		
		<!-- 功能菜单 -->
		<view class="menu-list">
			<view class="menu-item" @click="goParticipations">
				<text class="title">我的参与</text>
				<text class="arrow">></text>
			</view>
			<view class="menu-item" @click="goRewards">
				<text class="title">我的奖励</text>
				<text class="arrow">></text>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { onShow } from "@dcloudio/uni-app"

const BASE_URL = 'http://192.168.110.192:9003'
const userInfo = ref(null)

// 格式化日期
const formatDate = (dateStr) => {
	if (!dateStr) return ''
	const date = new Date(dateStr)
	return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 每次页面显示时都重新获取用户信息
onShow(() => {
	const info = uni.getStorageSync('userInfo')
	if(info) {
		userInfo.value = info
	} else {
		userInfo.value = null
	}
})

const goLogin = () => {
	uni.navigateTo({
		url: '/pages/user/login'
	})
}

const goParticipations = () => {
	if(!userInfo.value) {
		goLogin()
		return
	}
	uni.navigateTo({
		url: '/pages/user/participations'
	})
}

const goRewards = () => {
	if(!userInfo.value) {
		goLogin()
		return
	}
	uni.navigateTo({
		url: '/pages/user/rewards'
	})
}

const goSettings = () => {
	uni.navigateTo({
		url: '/pages/user/settings'
	})
}

const handleLogout = () => {
	uni.showModal({
		title: '提示',
		content: '确认退出登录？',
		success: (res) => {
			if(res.confirm) {
				uni.removeStorageSync('userInfo')
				uni.removeStorageSync('user_id')
				userInfo.value = null
				uni.showToast({
					title: '已退出登录',
					icon: 'success'
				})
			}
		}
	})
}
</script>

<style lang="scss" scoped>
.container {
	padding-top: calc(var(--status-bar-height) + 88rpx);
	background-color: #f5f5f5;
}

.custom-nav {
	position: fixed;
	top: var(--status-bar-height);
	left: 0;
	right: 0;
	z-index: 100;
	background-color: #fff;
	// padding-top: var(--status-bar-height);
	box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
	height: 88rpx;
	
	.nav-content {
		position: relative;
		height: 88rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		
		.title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333;
		}
		
		.settings {
			position: absolute;
			right: 30rpx;
			top: 50%;
			transform: translateY(-50%);
			padding: 10rpx;
		}
	}
}

.user-info {
	display: flex;
	align-items: center;
	padding: 40rpx;
	background: #fff;
	
	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 60rpx;
		margin-right: 30rpx;
	}
	
	.info {
		display: flex;
		flex-direction: column;
		
		.username {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 10rpx;
		}
		
		.points {
			font-size: 28rpx;
			color: #666;
			margin-bottom: 8rpx;
		}
		
		.join-time {
			font-size: 24rpx;
			color: #999;
		}
	}
}

.not-login {
	padding: 40rpx;
	background: #fff;
	text-align: center;
	font-size: 32rpx;
	color: #007AFF;
	display: flex;
	flex-direction: column;
	align-items: center;
	
	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 60rpx;
		margin-bottom: 20rpx;
	}
}

.menu-list {
	margin-top: 20rpx;
	background: #fff;
	
	.menu-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1rpx solid #eee;
		
		.title {
			font-size: 30rpx;
		}
		
		.arrow {
			color: #999;
		}
	}
}

.logout {
	margin-top: 60rpx;
	padding: 30rpx;
	background: #fff;
	text-align: center;
	font-size: 32rpx;
	color: #ff4d4f;
}
</style> 